package com.woniuxy.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.entity.RbacManager;
import com.woniuxy.entity.ResponseEntity;
import com.woniuxy.service.RbacManagerService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: 马宇航
 * @Do: 用户列表类
 * @DateTime: 22/05/23/0023 下午 2:33
 * @Component: 成都蜗牛学苑
 **/
@RestController
//切记不要写成manager
@RequestMapping("/rbacManager")
public class RbacManagerController {
    @Autowired
    RbacManagerService rbacManagerService;

    @GetMapping("/manager/{currentPage}/{pageSize}")
    //超级管理员或者普通管理员可以放心
    @RequiresRoles(value={"超级管理员","普通管理员"},logical = Logical.OR)
    public ResponseEntity findAll(@PathVariable int currentPage,@PathVariable int pageSize){
        PageHelper.startPage(currentPage,pageSize);
        List<RbacManager> all = rbacManagerService.findAll();
        //包含了分页信息的
        PageInfo<RbacManager> rbacManagerPageInfo = new PageInfo<>(all);
        return new ResponseEntity(rbacManagerPageInfo);
    }
    @DeleteMapping("/manager/{id}")
    @RequiresPermissions(value = {"role:del"})
    public ResponseEntity deleteOnedel(@PathVariable int id){
        int i = rbacManagerService.deleteOne(id);
        if (i>0){
           return ResponseEntity.SUCCESS;
        }else {
            return ResponseEntity.ERROR;
        }
    }

    @PostMapping("/manager")
    public ResponseEntity saveOne(@RequestBody RbacManager rbacManager){
        int i = 0;
        if (rbacManager.getId() == 0){
            //新增
            i = rbacManagerService.saveOne(rbacManager);
        }else {
            i = rbacManagerService.updateOne(rbacManager);
        }
        if (i>0){
            return ResponseEntity.SUCCESS;
        }else {
            return ResponseEntity.ERROR;
        }
    }

    @PutMapping(value = "/manager/{account}")
//    @RequiresPermissions(value = {"role:del"})
    public ResponseEntity resetPassword(@PathVariable String account){
        int i = rbacManagerService.resetPassword(account);
        if (i>0){
            return ResponseEntity.SUCCESS;
        }else {
            return ResponseEntity.ERROR;
        }
    }
}
